package com.liu.bookshop.book;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.liu.bookshop.config.CsvExportUtil;
import com.liu.bookshop.entities.Book;
import com.liu.bookshop.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.OutputStream;

@RestController
public class Main {

    Map<Book, Integer> map = new HashMap<>();

    @Autowired
    private SellMapper sellMapper;

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/books")
    public String getBooks(HttpServletResponse response) throws JsonProcessingException {
        response.addHeader("Access-Control-Allow-Origin", "*");
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(bookMapper.selectList(null));
    }

    @PostMapping("/book/classify")
    public String getBooksByClassify(@RequestParam("data") String classify,HttpServletResponse response) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        response.addHeader("Access-Control-Allow-Origin", "*");
        wrapper.eq("classify", classify);
        return mapper.writeValueAsString(bookMapper.selectList(wrapper));
    }

    @PostMapping("/book/auth")
    public String getBooksByAuth(@RequestParam("data") String auth,HttpServletResponse response) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        response.addHeader("Access-Control-Allow-Origin", "*");
        wrapper.eq("auth", auth);
        return mapper.writeValueAsString(bookMapper.selectList(wrapper));
    }

    @PostMapping("/book/publish")
    public String getBooksByPublish(@RequestParam("data") String publish,HttpServletResponse response) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        response.addHeader("Access-Control-Allow-Origin", "*");
        wrapper.eq("publish", publish);
        return mapper.writeValueAsString(bookMapper.selectList(wrapper));
    }

    @PostMapping("/book/bookName")
    public String getBooksByBookName(@RequestParam("data") String name, HttpServletResponse response) throws JsonProcessingException {
        response.addHeader("Access-Control-Allow-Origin", "*");
        ObjectMapper mapper = new ObjectMapper();
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        return mapper.writeValueAsString(bookMapper.selectList(wrapper));
    }

    @PostMapping("/login")
    public String Login(@RequestParam String username, @RequestParam String password, HttpServletResponse response) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        response.addHeader("Access-Control-Allow-Origin", "*");
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        wrapper.eq("password", password);
        Cookie cookie = new Cookie("username", username);
        response.addCookie(cookie);
        return mapper.writeValueAsString(userMapper.selectList(wrapper));
    }

    @PostMapping("/reset")
    public String Reset(@RequestParam("username") String username, @RequestParam("password") String password, HttpServletResponse response) throws JsonProcessingException {
        response.addHeader("Access-Control-Allow-Origin", "*");
        System.out.println(username + ":" + password);
        ObjectMapper mapper = new ObjectMapper();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = userMapper.selectOne(wrapper);
        user.setPassword(password);
        return mapper.writeValueAsString(userMapper.updateById(user));
    }

//    @GetMapping("/sell")
//    public String getSell() throws JsonProcessingException {
//        map.clear();
//        ObjectMapper mapper = new ObjectMapper();
//
//        List<Book> books = bookMapper.selectList(null);
//        for (Book book : books) {
//            QueryWrapper<Book> wrapper = new QueryWrapper<>();
//            wrapper.eq("name", book.getName());
//            Integer integer = bookMapper.selectCount(wrapper);
//            map.put(book, integer);
//        }
//        return mapper.writeValueAsString(map);
//    }

    @GetMapping("/sell")
    public String getSell(HttpServletResponse response) throws JsonProcessingException {
        response.addHeader("Access-Control-Allow-Origin", "*");
        return (new ObjectMapper()).writeValueAsString(sellMapper.getSell());
    }

    @GetMapping("/sell/csv")
    public void getSellCSV(HttpServletResponse response){
        response.addHeader("Access-Control-Allow-Origin", "*");
        List<Map<String, Object>> sell = sellMapper.getSell();
        String titles = "书名,出版社,价格,作者,分类,库存"; // 设置表头
        String keys = "name,publish,price,auth,classify,count";
        String fName = "book_";
        try {
            OutputStream os = response.getOutputStream();
            CsvExportUtil.responseSetProperties(fName, response);
            CsvExportUtil.doExport(sell, titles, keys, os);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
